﻿using System;
using System.Collections.Generic;
using CoreGraphics;
using UIKit;
using Foundation;

namespace Sino.PagingView.Touch
{
	public class PagingHeader : UIView
	{
		#region Event

		#endregion

		#region Field
		public static UIColor HighlightTitleColor = UIColor.FromRGB(84, 188, 117);
		public static UIColor NormalTitleColor = UIColor.FromRGB(166, 166, 166);
		public static UIFont TitleFont = UIFont.SystemFontOfSize(14);
		public static UIColor CursorColor = UIColor.FromRGB(84, 188, 117);


		private IPagingHost _host;
		private UILabel[] _titles;


		private nfloat[] _titleTextWidths;
		private nfloat[] _titleCursosX;
		private UIView _cursor;
		private bool _inited;
		#endregion

		#region Property



		#endregion

		#region Constructor

		public PagingHeader(IPagingHost host)
		{
			_host = host;
			_host.TitleChanged += _host_TitleChanged;
			InitView();
		}


		public PagingHeader(CGRect frame, IPagingHost host)
			: base(frame)
		{
			_host = host;
			_host.TitleChanged += _host_TitleChanged;
			InitView();
		}

		#endregion

		#region Method

		private void InitView()
		{
			if (Frame.Width <= 0)
				return;


			var titleWidth = Frame.Width / _host.Title.Length;

			if (titleWidth < 20)
				throw new NotImplementedException();

			if (_titles != null)
			{
				foreach (var view in _titles)
				{
					view.RemoveFromSuperview();
				}
				_cursor.RemoveFromSuperview();
			}

			_titleTextWidths = new nfloat[_host.Title.Length];
			_titles = new UILabel[_host.Title.Length];
			_titleCursosX = new nfloat[_host.Title.Length];

			for (int i = 0; i < _host.Title.Length; i++)
			{
				var size = _host.Title[i].StringSize(TitleFont);
				_titleTextWidths[i] = size.Width;
				_titleCursosX[i] = i * titleWidth + (titleWidth - size.Width) / 2;
				var label = new UILabel();
				AddSubview(label);
				label.TextAlignment = UITextAlignment.Center;
				label.Frame = new CGRect(i * titleWidth, 8, titleWidth, size.Height);
				label.Text = _host.Title[i];
				label.TextColor = NormalTitleColor;
				label.Font = TitleFont;
				label.Tag = i;
				_titles[i] = label;
				label.UserInteractionEnabled = true;
				label.AddGestureRecognizer(new UITapGestureRecognizer(_label_tag));
				//AddSubview(new UIView(new CGRect(_titleCursosX[i], size.Height + 1, _titleTextWidths[i], 2)){ BackgroundColor = CursorColor });
			}

			_cursor = new UIView(new CGRect(_titleCursosX[0], _titles[0].Frame.Height + 8 + 3, _titleTextWidths[0], 2)) { BackgroundColor = CursorColor };
			_titles[0].TextColor = HighlightTitleColor;
			AddSubview(_cursor);
			_host.ScrollTo(0, true);
			if (_inited) return;
			_host.Scrolling += _host_Scrolling;
			_host.IndexChanged += _host_IndexChanged;

			_inited = true;
		}

		void _label_tag(UITapGestureRecognizer g)
		{
			_host.ScrollTo((int)g.View.Tag, true);
		}

		void _host_IndexChanged(object sender, Touch.ScrollEventArgs e)
		{
			for (int i = 0; i < _titles.Length; i++)
			{
				_titles[i].TextColor = NormalTitleColor;
			}
			_titles[e.Index].TextColor = HighlightTitleColor;
		}

		void _host_Scrolling(object sender, Touch.ScrollEventArgs e)
		{
			var p = e.OffsetX / e.Total;

			var dx = _titleCursosX[0] + p * Frame.Width;

			_cursor.Frame = new CGRect(dx, _titles[0].Frame.Height + 8 + 3, _titleTextWidths[0], 2);

			foreach (var label in _titles)
			{
				label.TextColor = NormalTitleColor;
			}

			var index = (int)(e.OffsetX / e.BoundWidth);
			_titles[index].TextColor = InterpolationColor(HighlightTitleColor, NormalTitleColor, e.OffsetX / e.BoundWidth - index);
			if (index + 1 < _titles.Length)
				_titles[index + 1].TextColor = InterpolationColor(HighlightTitleColor, NormalTitleColor, index + 1 - e.OffsetX / e.BoundWidth);
		}

		void _host_TitleChanged(object sender, EventArgs e)
		{
			InitView();
		}
		#endregion

		private static UIColor InterpolationColor(UIColor color, UIColor endColor, nfloat factor)
		{
			var startComponent = color.CGColor.Components;
			var endComponent = endColor.CGColor.Components;

			var startAlpha = color.CGColor.Alpha;
			var endAlpha = endColor.CGColor.Alpha;

			var r = startComponent[0] + (endComponent[0] - startComponent[0]) * factor;
			var g = startComponent[1] + (endComponent[1] - startComponent[1]) * factor;
			var b = startComponent[2] + (endComponent[2] - startComponent[2]) * factor;
			var a = startAlpha + (endAlpha - startAlpha) * factor;

			return UIColor.FromRGBA(r, g, b, a);
		}
	}

	public class ScrollEventArgs : EventArgs
	{
		public nfloat OffsetX;
		public nfloat Total;
		public int Index;
		public nfloat BoundWidth;
	}

}

